<?php


if (isset($_GET['action']) && $_GET['action'] == "edit" && isset($_GET['download_id'])) {

	$download_id = $_GET['download_id'];

	$download_item = $download->get($download_id);

	$download_item = $download_item[0];

	$smarty->assign('download_item', $download_item);

	$smarty->assign('fileaction', "edit");

	$smarty->display('download_edit.tpl', $_SESSION['uid']);

}elseif (isset($_GET['action']) && $_GET['action'] == "new") {

	$smarty->assign('fileaction', "new");

	$smarty->display('download_edit.tpl', $_SESSION['uid']);

}elseif (isset($_GET['action']) && $_GET['action'] == "remove" && is_allowed(3))  {

	$download_id = $_GET['download_id'];

	$download->remove($download_id);

	$smarty->assign('message', "The download has been removed!");

	$downloads = $download->get();

	$smarty->assign('downloads', $downloads);

	$smarty->display('download_list.tpl', $_SESSION['uid']);

}elseif (isset($_POST['action']) && $_POST['action'] == "save") {


	$download_id 	= $_POST['download_id'];
	$name 			= $_POST['name'];
	$description 	= $_POST['description'];
	$user_level 	= $_POST['user_level'];

		$file_name 		= "";
		$file_size 		= "";
		$screenshot_name = "";

	if($_POST['fileaction'] == "edit") {

    	$download_item = $download->get($download_id);

    	$download_item = $download_item[0];

    	if($_FILES["download"]['error'] == 4) {



    			$file_name 		= $download_item['file_name'];
    			$file_size 		= $download_item['file_size'];

    	}
        if ($_FILES["screenshot"]['error'] == 4) {

    		$screenshot_name = $download_item['screenshot_name'];
        }

	}



	if(!empty($_FILES["download"])) {

		$downloaddir = $_SERVER['DOCUMENT_ROOT'] . "/downloads/";



		if(move_uploaded_file($_FILES["download"]["tmp_name"], $downloaddir . $_FILES["download"]["name"])) {

			$file_name 		= $_FILES["download"]["name"];
			$file_size 		= $_FILES['download']['size'];

			if(!empty($_FILES["screenshot"])) {
				$downloaddir = "./screenshots/";

				if(move_uploaded_file($_FILES["screenshot"]["tmp_name"], $uploaddir . $_FILES["screenshot"]["name"])) {

					$screenshot_name = $_FILES["screenshot"]["name"];

				} else {

				}
			}
		} else {

		}
	}


	$download->save($download_id, $file_name, $file_size, $name, $description, $screenshot_name, $user_level);

	$smarty->assign('message', "The news item has been changed/added!");

	$smarty->display('download_edit.tpl', $_SESSION['uid']);

} else {

	$downloads = $download->get();

	$smarty->assign('downloads', $downloads);

	$smarty->display('download_list.tpl', $_SESSION['uid']);
}





?>
